<template>
    <el-dialog
        class="hand-analyze-dialog"
        :visible.sync="isOpenDialog"
        center
        :close-on-click-modal="false"
        :title="'手牌分析'"
        :width="'1660px'"
    >
        <div class="hand-analyze">
            <bet-summary :betSummary="betSummary"></bet-summary>
            <el-switch
                v-model="ruleFlag"
                active-text="满足所有条件"
                inactive-text="满足任意条件"
            >
            </el-switch>
            <div class="bet-table">
                <el-table
                    class="hand-make-table"
                    :data="makeStrategy"
                    @selection-change="handleMakeSelectChange"
                    :height="'500px'"
                    :header-cell-style="{'text-align':'left'}"
                >
                    <el-table-column type="selection" width="45">
                    </el-table-column>
                    <el-table-column
                        v-for="item of tableColumn"
                        :key="item.prop"
                        :prop="item.prop"
                        :label="getLabel(item.label)"
                        :width="getTableColumnWidth(item.prop)"
                        sortable
                    ></el-table-column>
                </el-table>
                <el-table
                    class="hand-draw-table"
                    :data="drawStrategy"
                    @selection-change="handleDrawSelectChange"
                    :height="'500px'"
                    :header-cell-style="{'text-align':'left'}"
                >
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <el-table-column
                        v-for="item of tableColumn"
                        :key="item.prop"
                        :prop="item.prop"
                        :label="getLabel(item.label)"
                        :width="getTableColumnWidth(item.prop)"
                        sortable
                    ></el-table-column>
                </el-table>
            </div>
        </div>
    </el-dialog>
</template>

<script>
import {
    getRenderColor,
    getBetDesc,
    makeOrder,
    drawOrder,
    accumulateBetAndCombo,
    getMakeDesc,
    getDrawDesc,
} from "@/utils/poker/poker-config";
import BetSummary from "@/components/bet-summary";
export default {
    name: "HandAnalyze",
    components: {
        BetSummary,
    },
    props: {
        strategies: {
            type: Object,
        },
        boards: {
            type: Array,
        },
        isOpen: {
            type: Boolean,
        },
    },
    data() {
        return {
            selectedMake: [],
            selectedDraw: [],
            ruleFlag: true,
        };
    },
    computed: {
        makeStrategy() {
            let strategy = {};
            let strategyArray = [];
            if (this.boards.length == 0) {
                return strategyArray;
            }
            for (let board of this.boards) {
                for (let hand in this.strategies[board]) {
                    accumulateBetAndCombo(
                        "make",
                        strategy,
                        this.strategies[board][hand]
                    );
                }
            }
            for (let makeKey in strategy) {
                let bet = strategy[makeKey].bet;
                for (let betKey in bet) {
                    bet[betKey] = parseFloat(
                        (bet[betKey] / strategy[makeKey].combos).toFixed(2)
                    );
                }
                strategyArray.push({
                    hand_spec: makeKey,
                    hand_desc: getMakeDesc(makeKey),
                    bet,
                    combos: parseFloat(strategy[makeKey].combos.toFixed(2)),
                });
            }
            strategyArray.sort((a, b) => {
                return (
                    makeOrder.indexOf(a.hand_spec) -
                    makeOrder.indexOf(b.hand_spec)
                );
            });
            return strategyArray;
        },

        drawStrategy() {
            let strategy = {};
            let strategyArray = [];
            if (this.boards.length == 0) {
                return strategyArray;
            }
            for (let board of this.boards) {
                for (let hand in this.strategies[board]) {
                    accumulateBetAndCombo(
                        "draw",
                        strategy,
                        this.strategies[board][hand]
                    );
                }
            }
            for (let drawKey in strategy) {
                let bet = strategy[drawKey].bet;
                for (let betKey in bet) {
                    bet[betKey] = parseFloat(
                        (bet[betKey] / strategy[drawKey].combos).toFixed(2)
                    );
                }
                strategyArray.push({
                    hand_spec: drawKey,
                    hand_desc: getDrawDesc(drawKey),
                    bet,
                    combos: parseFloat(strategy[drawKey].combos.toFixed(2)),
                });
            }
            strategyArray.sort((a, b) => {
                return (
                    drawOrder.indexOf(a.hand_spec) -
                    drawOrder.indexOf(b.hand_spec)
                );
            });
            return strategyArray;
        },
        tableColumn() {
            let tableColumn = [
                {
                    label: "手牌特征",
                    prop: "hand_desc",
                },
                {
                    label: "组合数",
                    prop: "combos",
                },
            ];
            if (this.makeStrategy.length > 0) {
                for (let betKey in this.makeStrategy[0].bet) {
                    tableColumn.push({
                        label: betKey,
                        prop: `bet.${betKey}`,
                    });
                }
            }
            return tableColumn;
        },
        isOpenDialog: {
            get() {
                return this.isOpen;
            },
            set() {
                this.closeDialog();
            },
        },
        betSummary() {
            let betSummary = {};
            for (let board of this.boards) {
                for (let hand in this.strategies[board]) {
                    this.accumulateBetSummary(
                        betSummary,
                        this.strategies[board][hand]
                    );
                }
            }
            for (let betKey in betSummary) {
                betSummary[betKey].percent = parseFloat(
                    (
                        betSummary[betKey].percent /
                        betSummary[betKey].totalCombos
                    ).toFixed(2)
                );
                betSummary[betKey].combos = parseFloat(
                    betSummary[betKey].combos.toFixed(2)
                );
            }
            return betSummary;
        },
    },
    methods: {
        closeDialog() {
            this.$emit("update:isOpen", false);
        },

        accumulateBetSummary(betSummary, strategyBoardHand) {
            let flag = false;
            if (
                this.selectedMake.length == 0 &&
                this.selectedDraw.length == 0
            ) {
                flag = true;
            } else {
                if (this.ruleFlag) {
                    let checkMakeSucc = false;
                    if (this.selectedMake.length > 0) {
                        if (!strategyBoardHand.desc.make) {
                            if (
                                !(
                                    this.selectedMake.length === 1 &&
                                    this.selectedMake[0] == "no_make"
                                )
                            ) {
                                return;
                            } else {
                                checkMakeSucc = true;
                            }
                        }
                        if (!checkMakeSucc) {
                            for (let handSpec of this.selectedMake) {
                                if (!strategyBoardHand.desc.make[handSpec]) {
                                    return;
                                }
                            }
                        }
                    }
                    if (this.selectedDraw.length > 0) {
                        if (!strategyBoardHand.desc.draw) {
                            if (
                                !(
                                    this.selectedDraw.length === 1 &&
                                    this.selectedDraw[0] == "no_draw"
                                )
                            ) {
                                return;
                            } else {
                                flag = true;
                            }
                        }
                        if (!flag) {
                            for (let handSpec of this.selectedDraw) {
                                if (!strategyBoardHand.desc.draw[handSpec]) {
                                    return;
                                }
                            }
                        }
                    }
                    flag = true;
                } else {
                    do {
                        if (this.selectedMake.length > 0) {
                            if (
                                !strategyBoardHand.desc.make &&
                                this.selectedMake.indexOf("no_make") !== -1
                            ) {
                                flag = true;
                                break;
                            }
                            if (strategyBoardHand.desc.make) {
                                for (let handSpec of this.selectedMake) {
                                    if (strategyBoardHand.desc.make[handSpec]) {
                                        flag = true;
                                        break;
                                    }
                                }
                                if (flag) {
                                    break;
                                }
                            }
                        }
                        if (this.selectedDraw.length > 0) {
                            if (
                                !strategyBoardHand.desc.draw &&
                                this.selectedDraw.indexOf("no_draw") !== -1
                            ) {
                                flag = true;
                                break;
                            }
                            if (strategyBoardHand.desc.draw) {
                                for (let handSpec of this.selectedDraw) {
                                    if (strategyBoardHand.desc.draw[handSpec]) {
                                        flag = true;
                                        break;
                                    }
                                }
                            }
                        }
                    } while (0);
                }
            }

            if (!flag) {
                return;
            }
            if (Object.keys(betSummary) == 0) {
                for (let betKey in strategyBoardHand.bet) {
                    betSummary[betKey] = {
                        percent:
                            strategyBoardHand.bet[betKey] *
                            strategyBoardHand.combos,
                        combos:
                            (strategyBoardHand.combos *
                                strategyBoardHand.bet[betKey]) /
                            100,
                        totalCombos: strategyBoardHand.combos,
                    };
                }
            } else {
                for (let betKey in strategyBoardHand.bet) {
                    betSummary[betKey].percent =
                        betSummary[betKey].percent +
                        strategyBoardHand.bet[betKey] *
                            strategyBoardHand.combos;
                    betSummary[betKey].totalCombos =
                        betSummary[betKey].totalCombos +
                        strategyBoardHand.combos;
                    betSummary[betKey].combos =
                        betSummary[betKey].combos +
                        (strategyBoardHand.combos *
                            strategyBoardHand.bet[betKey]) /
                            100;
                }
            }
        },

        handleMakeSelectChange(val) {
            this.selectedMake = val.map((item) => {
                return item.hand_spec;
            });
        },

        handleDrawSelectChange(val) {
            this.selectedDraw = val.map((item) => {
                return item.hand_spec;
            });
        },

        getCardStyle(key, val) {
            return {
                background: getRenderColor(key),
            };
        },

        getCardWidth() {
            let betCounts = Object.keys(this.betSummary).length;
            let lineItemCount = Math.ceil(betCounts / 2);
            return `${100 / lineItemCount}%`;
        },

        getBarStyle(key, val) {
            return {
                background: getRenderColor(key),
                width: `${val.percent}%`,
            };
        },

        getTableColumnWidth(prop) {
            switch (prop) {
                case "hand_desc":
                    return "110px";
                case "combos":
                    return "90px";
                case "bet.r_a":
                case "bet.r_m":
                case "bet.r_s":
                case "bet.cl":
                case "bet.f":
                    return "90px"
                case "bet.r_b":
                    return "100px"
            }
            return null;
        },

        getLabel(label) {
            let desc = getBetDesc(label);
            return desc ? desc : label;
        },
    },
};
</script>

<style lang="scss">
.hand-analyze {
    .bet-table {
        display: flex;
    }

    .el-table__row {
        height: 40px;
    }
}
</style>
